package com.purchase.ml.service.prod.impl;

import com.purchase.ml.consts.CommonConsts;
import com.purchase.ml.consts.Constants;
import com.purchase.ml.entity.dealer.SysDealer;
import com.purchase.ml.entity.dealer.SysDealerCategory;
import com.purchase.ml.entity.dealer.SysDealerPruduct;
import com.purchase.ml.entity.order.SysOrder;
import com.purchase.ml.entity.order.SysOrderDetail;
import com.purchase.ml.entity.prod.SysCategory;
import com.purchase.ml.entity.prod.SysProduct;
import com.purchase.ml.entity.prod.SysProductSku;
import com.purchase.ml.enums.OrderStatusEnum;
import com.purchase.ml.exception.BizException;
import com.purchase.ml.exception.CommonEnum;
import com.purchase.ml.mapper.dealer.DealerCategoryMapper;
import com.purchase.ml.mapper.dealer.DealerProductMapper;
import com.purchase.ml.mapper.dealer.SysDealerMapper;
import com.purchase.ml.mapper.order.SysOrderDetailMapper;
import com.purchase.ml.mapper.order.SysOrderMapper;
import com.purchase.ml.mapper.prod.SysCategoryMapper;
import com.purchase.ml.mapper.prod.SysProdMapper;
import com.purchase.ml.mapper.prod.SysProdSkuMapper;
import com.purchase.ml.service.prod.DealerProductService;
import com.purchase.ml.service.prod.SysCategoryService;
import com.purchase.ml.service.prod.SysProdService;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.apache.bcel.classfile.ConstantString;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Locale;

/**
 * @author :zxm
 * @date :2021/3/11 16:53
 * @description :主经销商/经销商购买商品成功后，将其购买商品信息保存
 **/
@Service
@Slf4j
public class DealerProductServiceImpl implements DealerProductService {

    @Autowired
    private SysOrderMapper orderMapper;
    @Autowired
    private SysOrderDetailMapper orderDetailMapper;
    @Autowired
    private SysProdMapper productMapper;
    @Autowired
    private SysCategoryMapper categoryMapper;
    @Autowired
    private DealerCategoryMapper dealerCategoryMapper;
    @Autowired
    private DealerProductMapper dealerProductMapper;
    @Autowired
    private SysProdSkuMapper prodSkuMapper;

    @Autowired
    private SysDealerMapper sysDealerMapper;

    // 创建的订单库存应该已判断完毕
    @Transactional
    @Override
    public void changeMasterDealerStock(Long oid) {
        log.info("oid============{}",oid);
        /*仅能在住经销商购买的时候创创建*/
        // 获取订单
        SysOrder dbOrder = orderMapper.findOrderByOid(oid);
        // 获取订单详情
        List<SysOrderDetail> dbDetailList = orderDetailMapper.findDetailByOid(oid);
        // 根据商品id，给购买者设置商品目录和商品
        for (SysOrderDetail orderDetail:dbDetailList) {
            SysProduct dbProduct = productMapper.selectProductByProductId(orderDetail.getProductId());
            SysCategory dbCategory = categoryMapper.findCategoryByCategoryId(dbProduct.getCategoryId());
            // 保存购买者的商品目录及商品信息
            // 判断购买者是否存在该商品分类信息
            // 判断经销商的类型;决定总库存的增减；还是商户库存的增减；当订单的状态是成功的时候
            if(dbOrder.getDealerType() == CommonConsts.ROLETYPE_MASTER && dbOrder.getState().equals(OrderStatusEnum.PAID.getCode())){
                SysDealerCategory dbDealerCategory = dealerCategoryMapper.findBuyerCategoryByDealerId(dbOrder.getBuyer(), dbProduct.getCategoryId());
                if(dbDealerCategory == null){
                    SysDealerCategory category = new SysDealerCategory();
                    category.setCategoryId(dbCategory.getCategoryId());
                    category.setDealerId(dbOrder.getBuyer());
                    category.setState(Constants.NORMAL_STATE);
                    category.setIndex(dbCategory.getIndex());  //索引为系统索引
                    dealerCategoryMapper.addDealerCategory(category);
                }
                //判断购买者是否存在该商品
                SysDealerPruduct dbDealerProduct = dealerProductMapper.findBuyerProductByDealerId(dbOrder.getBuyer(), dbProduct.getProductId());
                if(dbDealerProduct == null){
                    SysDealerPruduct product = new SysDealerPruduct();
                    product.setProductId(dbProduct.getProductId());
                    product.setDealerId(dbOrder.getBuyer());
                    SysProductSku productSku = prodSkuMapper.selectProductSkuByProductId(orderDetail.getProductId());
                    product.setStock(orderDetail.getQuantity() * productSku.getExchangeNum());
                    product.setDeliveryDay(productSku.getDeliveryDay());
                    product.setMoq(productSku.getMoq());
                    product.setState(Constants.NORMAL_STATE);
                    dealerProductMapper.changeMasterDealerStock(product);
                }else {
                    //查询出对应商品SKU的兑换数量
                    SysProductSku productSku = prodSkuMapper.selectProductSkuByProductId(orderDetail.getProductId());
                    // 增加商户商品库存；根据兑换比例
                    dealerProductMapper.updateDealerProduct(dbOrder.getBuyer(),dbProduct.getProductId(),orderDetail.getQuantity() * productSku.getExchangeNum());
                }
                prodSkuMapper.reduceSysProductStock(dbProduct.getProductId(),orderDetail.getQuantity());
            }else{
                // 经销商购买成功后，减少主经销商库存
                dealerProductMapper.reduceMasterStock(dbOrder.getSellers(), dbProduct.getProductId(),orderDetail.getQuantity());
            }
        }
    }
}
